package god.dream.loginfo.controller;

import god.dream.loginfo.model.LogInfo;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.ModelAndViewDefiningException;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import org.springframework.web.util.WebUtils;

public class AdminCheckInterceptor extends HandlerInterceptorAdapter {
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		LogInfo logInfo = (LogInfo) WebUtils.getSessionAttribute(request, "logInfo");
		
		if(logInfo == null){
			ModelAndView mav = new ModelAndView("redirect:/cont/login.htm");
			throw new ModelAndViewDefiningException(mav);
		}
		if(!logInfo.isAdministrator()){
			ModelAndView mav = new ModelAndView("redirect:/cont/listUsers.htm");
			throw new ModelAndViewDefiningException(mav);
		}
		
		
		return true;
	}

}
